#include "scoresprite.h"
#include<QPainter>
#include<QGraphicsScene>
QImage ScoreSprite::ScoreFonts[10]
{
    QImage(":/fonts/Images/fonts/font_048.png"),
    QImage(":/fonts/Images/fonts/font_049.png"),
    QImage(":/fonts/Images/fonts/font_050.png"),
    QImage(":/fonts/Images/fonts/font_051.png"),
    QImage(":/fonts/Images/fonts/font_052.png"),
    QImage(":/fonts/Images/fonts/font_053.png"),
    QImage(":/fonts/Images/fonts/font_054.png"),
    QImage(":/fonts/Images/fonts/font_055.png"),
    QImage(":/fonts/Images/fonts/font_056.png"),
    QImage(":/fonts/Images/fonts/font_057.png"),
};
void ScoreSprite::scoreAdd()
{
    score++;
}

void ScoreSprite::onGameBegin()
{
}

void ScoreSprite::onGameOver()
{
}

void ScoreSprite::onGameRestart()
{
    score=0;
}

ScoreSprite::ScoreSprite(QGraphicsScene*scene)
{
    score=0;
    connect(&globalGameMsger::m,&globalGameMsger::gameBegin,this,&ScoreSprite::onGameBegin);
    connect(&globalGameMsger::m,&globalGameMsger::gameOver,this,&ScoreSprite::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameRestart,this,&ScoreSprite::onGameRestart);
    connect(&globalGameMsger::m,&globalGameMsger::addScore,this,&ScoreSprite::scoreAdd);
    scene->addItem(this);
    setZValue(1);
    setPos(scene->sceneRect().width()/2.0,scene->sceneRect().height()*0.05);
}

void ScoreSprite::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
    int tmp=score,post=boundingRect().width();
    do
    {
        post-=ScoreFonts[tmp%10].rect().width();
        painter->drawImage(post,0,ScoreFonts[tmp%10]);
        tmp/=10;
    }while(tmp);
}

QRectF ScoreSprite::boundingRect() const
{
    int hi=ScoreFonts[0].rect().height();
    int wi=0,tmp=score;
    do
    {
        wi+=ScoreFonts[tmp%10].rect().width();
        tmp/=10;
    }while(tmp);
    return QRectF(-wi/2.0,-hi/2.0,wi,hi);
}
void ScoreSprite::advance(int phase)
{
    if(phase==0)return;
}

QPainterPath ScoreSprite::shape() const
{
    return QPainterPath();
}
